package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.config.redis.RedisKeyConstant;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.WorkOrderDao;
import com.egoo.ticket.server.dao.WorkOrderUrgeDao;
import com.egoo.ticket.server.pojo.dto.WorkOrderUrgeInputDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderUrgeOutDto;
import com.egoo.ticket.server.pojo.entity.WorkOrder;
import com.egoo.ticket.server.pojo.entity.WorkOrderUrge;
import com.egoo.ticket.server.service.WorkOrderUrgeService;
import com.egoo.ticket.server.utils.common.*;
import com.google.common.base.Splitter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author Donny_dong
 */
@RestController
@RequestMapping(value = "/v1/workOrderUrge", produces = "application/json")
@Api(value = "催单管理",tags = "催单管理",description = "催单管理")
public class WorkOrderUrgeController {

	@Autowired
	private WorkOrderUrgeService workOrderUrgeService;
	@Autowired
	private WorkOrderDao workOrderDao;
	@Autowired
	private EwsUserDao ewsUserDao;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private WorkOrderUrgeDao workOrderUrgeDao;

	@ApiOperation(value = "根据workOrderId获取催单信息", notes = "根据workOrderId获取催单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectContentById")
	public DbResponse selectContentById(@ApiParam(value = "workOrderId", required = true) @RequestParam(name = "workOrderId") String workOrderId) {
		DbResponse dbResponse = new DbResponse();
		List<WorkOrderUrgeOutDto> workOrderUrgeOutDtoListList = workOrderUrgeService.selectByWorkOrderId(workOrderId);
		dbResponse.setRetCode(200);
		dbResponse.setData(workOrderUrgeOutDtoListList);
		return dbResponse;
	}

	@ApiOperation(value = "批量新建催单信息", notes = "批量新建催单信息")
	@RequestMapping(method = RequestMethod.POST, value = "/insertBatch")
	public DbResponse insertBatch(@ApiParam(value = "workOrderUrge", required = true) @RequestBody WorkOrderUrgeInputDto workOrderUrgeInputDto,
								  HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "催单失败");

         if (null != workOrderUrgeInputDto.getWorkOrderIdList()&&!workOrderUrgeInputDto.getWorkOrderIdList().isEmpty()) {
			 String userId = workOrderUrgeInputDto.getUserId();
			 for (int i = 0; i <workOrderUrgeInputDto.getWorkOrderIdList().size() ; i++) {
				 String workOrderId = workOrderUrgeInputDto.getWorkOrderIdList().get(i);
				 WorkOrder workOrder = workOrderDao.selectById(workOrderId);
				 String currentHandlerId = workOrder.getCurrentHandlerId();
				 List<String> list = Splitter.on(",").splitToList(currentHandlerId);
				 if (list.contains(userId+"")){
					 dbResponse.setMsg("催单失败：选择的工单中有当前处理人包含自己的工单");
					 return dbResponse;
				 }
				 boolean b = checkUrgTimeInterval(workOrderId,userId);
				 if (!b){
				 	return new DbResponse(ResponseConstant.RES_FAIL_CODE,"催单时间未超过指定时间，不能催单");
				 }
			 }
			 workOrderUrgeInputDto.setTenantId(tenantId);
			 Integer retCode = workOrderUrgeService.insertWorkOrderUrgeBatch(workOrderUrgeInputDto);
			 if (retCode >= 1) {
				 dbResponse.setRetCode(200);
				 dbResponse.setMsg("催单成功");
			 }
		 }else {
         	dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
         	dbResponse.setMsg("请选择工单");
		 }
		return dbResponse;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 判断是否有催单时间设定
	 * @Param:
	 * @return:
	 * @Date: 2020/6/3
	 */
	private boolean checkUrgTimeInterval(String workOrderId,String userId) {
		boolean check= false;
		String redisValue = redisUtil.getStr(RedisKeyConstant.WORK_ORDER_URGE);
//		redisValue ="2";
		if (StringUtils.isEmpty(redisValue)){
			return check =true;
		}

		HashMap<String, Object> hashMap = new HashMap<>(4);
		hashMap.put("userId",userId);
		hashMap.put("workOrderId",workOrderId);
		List<WorkOrderUrgeOutDto> workOrderUrgeOutDtos = workOrderUrgeDao.selectDynamic(hashMap);
		if(CollectionUtils.isEmpty(workOrderUrgeOutDtos)){
			return check =true;
		}
		WorkOrderUrgeOutDto workOrderUrgeOutDto = workOrderUrgeOutDtos.get(0);
		String createTime = workOrderUrgeOutDto.getCreateTime();
		long currentTimeMillis = System.currentTimeMillis();
		Long timeByDateStr = DateHandleUtil.getTimeByDateStr(createTime);
		long timeInterval = currentTimeMillis - timeByDateStr;
		Long timeIntervalSet = Long.valueOf(redisValue)*60*1000;
		if (timeInterval > timeIntervalSet){
			check = true;
		}
		return check;

	}

	@ApiOperation(value = "关闭催单信息", notes = "关闭催单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/delete")
	public DbResponse delete(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id) {
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "关闭失败");
		WorkOrderUrge workOrderUrge = workOrderUrgeService.selectById(id);
		if (workOrderUrge == null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("该催单信息不存在");
			return dbResponse;
		}
		Integer retCode = workOrderUrgeService.closeWorkOrderUrge(id);
		if (1 == retCode) {
			dbResponse.setRetCode(200);
			dbResponse.setMsg("关闭成功");
		}
		return dbResponse;

	}

}
